﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LogPrinter
{
    public static class LogPrinter
    {
        //获取指定的调试标记
        public static Boolean getDebugFlag(String section, String filePath)
        {
            Boolean isDebug = false;
            try
            {
                String debugFlag = "0";

                //首先从缓存类中获取，避免每次读取文件
                if (StaticRes.getInstance().Dic_DebugFlag.Keys.Contains(section))
                {
                    StaticRes.getInstance().Dic_DebugFlag.TryGetValue(section, out debugFlag);
                    if (debugFlag.Trim().Equals("1"))
                    {
                        isDebug = true;
                    }
                    else
                    {
                        isDebug = false;
                    }
                }
                else
                {
                    //获取配置文件
                    if (!File.Exists(filePath))
                    {
                        //如果没有配置文件则直接创建配置文件并且写入debug为0
                        FileRWDAO.FileRWDAO.WriteIniData(section, "debug", "0", filePath);
                    }
                    debugFlag = FileRWDAO.FileRWDAO.readIniData(section, "debug", filePath);
                    if (debugFlag != null && debugFlag != String.Empty)
                    {
                        if (debugFlag.Trim().Equals("1"))
                        {
                            StaticRes.getInstance().Dic_DebugFlag.Add(section, "1");
                            isDebug = true;
                        }
                        else
                        {
                            StaticRes.getInstance().Dic_DebugFlag.Add(section, "0");
                            isDebug = false;
                        }
                    }
                    else
                    {
                        FileRWDAO.FileRWDAO.WriteIniData(section, "debug", "0", filePath);
                        StaticRes.getInstance().Dic_DebugFlag.Add(section, "0");
                    }
                }
                return isDebug;
            }
            catch (Exception e1)
            {
                try
                {
                    File.AppendAllText("log_err.log", "----" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "---\r\nprintDebugLog2File(String logMsg)发生错误，错误信息：" + e1.Message + ":" + e1.StackTrace + "\r\n\r\n");
                }
                catch (Exception eq) { }
                return false;
            }
        }


        //直接输出Error日志，不需要检测debug标记
        /// <summary>
        /// 直接输出日常日志到当前的log目录下的modelName.log
        /// </summary>
        /// <param name="logMsg">日志String</param>
        /// <param name="modelName">模块名称String</param>
        public static void printGeneralLog2File(String logMsg, String modelName)
        {
            try
            {
                String filePath = AppDomain.CurrentDomain.BaseDirectory;
                filePath = filePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? filePath + "log" : filePath + Path.DirectorySeparatorChar.ToString() + "log";
                String filePathName = filePath + Path.DirectorySeparatorChar.ToString() + "" + modelName + ".log";
                //判断文件夹存在与否，不存在则创建
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                //判断文件是否存在，不存在则创建
                if (!File.Exists(filePathName))
                {
                    File.Create(filePathName).Close();
                }
                FileStream fs = File.Open(filePathName, FileMode.Open);
                long len = fs.Length;
                fs.Close();
                if (len >= 1024 * 1024)
                {
                    File.Move(filePathName, filePath + Path.DirectorySeparatorChar.ToString() + "" + modelName + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log");
                    File.Create(filePathName);
                }
                //追加内容到文件
                logMsg = "-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-----\r\n" + logMsg + "\r\n\r\n";
                File.AppendAllText(filePathName, logMsg);
            }
            catch (Exception e1)
            {
                try
                {
                    File.AppendAllText("log_err.log", "----" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "---\r\nprintDebugLog2File(String logMsg)发生错误，错误信息：" + e1.Message + ":" + e1.StackTrace + "\r\n\r\n");
                }
                catch (Exception eq) { }
            }
        }

        //直接输出Error日志，不需要检测debug标记
        /// <summary>
        /// 直接输出Error日志到当前的log目录下的err_modelName.log
        /// </summary>
        /// <param name="logMsg">日志String</param>
        /// <param name="modelName">模块名称String</param>
        public static void printErrorLog2File(String logMsg, String modelName)
        {
            String filePath = AppDomain.CurrentDomain.BaseDirectory;
            filePath = filePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? filePath + "log" : filePath + Path.DirectorySeparatorChar.ToString() + "log";
            String filePathName = filePath + Path.DirectorySeparatorChar.ToString() + "err_" + modelName + ".log";
            //判断文件夹存在与否，不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //判断文件是否存在，不存在则创建
            if (!File.Exists(filePathName))
            {
                File.Create(filePathName).Close();
            }
            FileStream fs = File.Open(filePathName, FileMode.Open);
            long len = fs.Length;
            fs.Close();
            if (len >= 1024 * 1024)
            {
                File.Move(filePathName, filePath + Path.DirectorySeparatorChar.ToString() + "err_" + modelName + ".log" + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log");
                File.Create(filePathName);
            }
            //追加内容到文件
            logMsg = "----error_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-----\r\n" + logMsg + "\r\n\r\n";
            File.AppendAllText(filePathName, logMsg);
        }
        //输出debuglog到默认log文件中
        /// <summary>
        /// 输出debuglog到log文件夹中的debug_modelName.log文件中
        /// </summary>
        /// <param name="logMsg">日志信息String</param>
        /// <param name="modelName">模块名称String</param>
        /// <param name="iniFilePath">配置文件，当前路径下</param>
        public static void printDebugLog2File(String logMsg, String modelName, String iniFilePath)
        {
            try
            {
                // File.AppendAllText("log_err.log", "LogPrinter printDebugLog2File()正在读取配置文件" + iniFilePath + "！");
                //如果获取到的debug标志为1则代表需要输出debug文件；
                if (!getDebugFlag(modelName, iniFilePath))
                {
                    return;
                }
                String filePath = AppDomain.CurrentDomain.BaseDirectory;
                filePath = filePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? filePath + "log" : filePath + Path.DirectorySeparatorChar.ToString() + "log";
                String filePathName = filePath + Path.DirectorySeparatorChar.ToString() + "debug_" + modelName + ".log";
                //判断文件夹存在与否，不存在则创建
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                //判断文件是否存在，不存在则创建
                if (!File.Exists(filePathName))
                {
                    File.Create(filePathName).Close();
                }
                FileStream fs = File.Open(filePathName, FileMode.Open);
                long len = fs.Length;
                fs.Close();
                if (len >= 1024 * 1024)
                {
                    File.Move(filePathName, filePath + Path.DirectorySeparatorChar.ToString() + "debug_" + modelName + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log");
                    File.Create(filePathName);
                }
                //追加内容到文件
                logMsg = "-----debug_" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "-----\r\n" + logMsg + "\r\n\r\n";
                File.AppendAllText(filePathName, logMsg);
            }
            catch (Exception e1)
            {
                try
                {
                    File.AppendAllText("log_err.log", "----" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "---\r\nprintDebugLog2File(String logMsg)发生错误，错误信息：" + e1.Message + ":" + e1.StackTrace + "\r\n\r\n");
                }
                catch (Exception eq) { }
            }
        }
        //输出debuglog到默认log文件中
        /// <summary>
        /// 输出debuglog到log文件夹中的debug_modelName.log文件中，debug标记有workplatm.ini决定
        /// </summary>
        /// <param name="logMsg">日志信息String</param>
        /// <param name="modelName">模块名称String</param> 
        //输出debuglog到默认log文件中
        public static void printModDebugLog2FilePoweredByWrokPlatm(String logMsg, String modelName)
        {
            try
            {
                String iniFilePath = AppDomain.CurrentDomain.BaseDirectory + "ini\\workplatm.ini";
                //如果获取到的debug标志为1则代表需要输出debug文件；
                if (!getDebugFlag("workplatm", iniFilePath))
                {
                    return;
                }
                else
                {
                    String filePath = AppDomain.CurrentDomain.BaseDirectory;
                    filePath = filePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? filePath + "log" : filePath + Path.DirectorySeparatorChar.ToString() + "log";
                    String filePathName = filePath + Path.DirectorySeparatorChar.ToString() + "debug_" + modelName + ".log";
                    //判断文件夹存在与否，不存在则创建
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    //判断文件是否存在，不存在则创建
                    if (!File.Exists(filePathName))
                    {
                        File.Create(filePathName).Close();
                    }
                    FileStream fs = File.Open(filePathName, FileMode.Open);
                    long len = fs.Length;
                    fs.Close();
                    if (len >= 1024 * 1024)
                    {
                        File.Move(filePathName, filePath + Path.DirectorySeparatorChar.ToString() + "debug_" + modelName + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log");
                        File.Create(filePathName);
                    }
                    //追加内容到文件
                    logMsg = "-----debug_" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "-----\r\n" + logMsg + "\r\n\r\n";
                    File.AppendAllText(filePathName, logMsg);
                }
            }
            catch (Exception e1)
            {
                try
                {
                    File.AppendAllText("log_err.log", "----" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "---\r\nprintDebugLog2File(String logMsg)发生错误，错误信息：" + e1.Message + ":" + e1.StackTrace + "\r\n\r\n");
                }
                catch (Exception eq) { }
            }
        }
        //输出debuglog到默认log文件中
        /// <summary>
        /// 输出debuglog到log文件夹中的debug_modelName.log文件中
        /// </summary>
        /// <param name="logMsg">日志信息String</param>
        /// <param name="modelName">模块名称String</param> 
        //输出debuglog到默认log文件中
        public static void printModDebugLog2File(String logMsg, String modelName)
        {
            try
            {
                String iniFilePath = AppDomain.CurrentDomain.BaseDirectory + "ini\\" + modelName + ".ini";
                //如果获取到的debug标志为1则代表需要输出debug文件；
                if (!getDebugFlag(modelName, iniFilePath))
                {
                    return;
                }
                else
                {
                    String filePath = AppDomain.CurrentDomain.BaseDirectory;
                    filePath = filePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? filePath + "log" : filePath + Path.DirectorySeparatorChar.ToString() + "log";
                    String filePathName = filePath + Path.DirectorySeparatorChar.ToString() + "debug_" + modelName + ".log";
                    //判断文件夹存在与否，不存在则创建
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    //判断文件是否存在，不存在则创建
                    if (!File.Exists(filePathName))
                    {
                        File.Create(filePathName).Close();
                    }
                    FileStream fs = File.Open(filePathName, FileMode.Open);
                    long len = fs.Length;
                    fs.Close();
                    if (len >= 1024 * 1024)
                    {
                        File.Move(filePathName, filePath + Path.DirectorySeparatorChar.ToString() + "debug_" + modelName + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log");
                        File.Create(filePathName);
                    }
                    //追加内容到文件
                    logMsg = "-----debug_" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "-----\r\n" + logMsg + "\r\n\r\n";
                    File.AppendAllText(filePathName, logMsg);
                }
            }
            catch (Exception e1)
            {
                try
                {
                    File.AppendAllText("log_err.log", "----" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss") + "---\r\nprintDebugLog2File(String logMsg)发生错误，错误信息：" + e1.Message + ":" + e1.StackTrace + "\r\n\r\n");
                }
                catch (Exception eq) { }
            }
        }
    }
}
